package com.lokiy.kit.fragment

import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.recyclerview.widget.RecyclerView
import com.lokiy.core.fragment.ViewBindingFragment
import com.lokiy.kit.R
import com.lokiy.kit.databinding.FragmentDanmuBinding
import com.lokiy.kit.databinding.FragmentDanmuItemBinding
import com.lokiy.annotation.FragmentInfo
import com.lokiy.kit.ext.setupToolbar
import com.lokiy.kit.widget.LinearDanMu
import kotlin.math.min
import kotlin.random.Random

/**
 *
 * @author Lokiy
 * @date 2023-08-04 14:44
 */
@FragmentInfo("弹幕")
class DanmuFragment : ViewBindingFragment<FragmentDanmuBinding>() {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        setupToolbar(binding.toolbar)
        binding.danmu.setup(
            LinearDanMu.TrackItem(Adapter(), 3),
            LinearDanMu.TrackItem(Adapter(), 4),
            LinearDanMu.TrackItem(Adapter(), 5)
        )
        binding.include.imageView.clipToOutline = true
    }

    private class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        private val values = mutableListOf<String>().apply {
            repeat(30) { i ->
                val sb = StringBuilder()
                repeat(Random.nextInt(20) + 1) { sb.append(i) }
                add(sb.toString())
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
            Log.e("XXX", "--------------Thread:" + Thread.currentThread().name)
            return Holder(FragmentDanmuItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        }

        override fun getItemCount(): Int = Int.MAX_VALUE

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            holder as Holder
            holder.binding.imageView.setImageDrawable(CircleDrawable(ResourcesCompat.getDrawable(holder.itemView.context.resources, R.drawable.a1, holder.itemView.context.theme)?.toBitmap()!!))
            holder.binding.textView.text = values[position % values.size]
            if (holder.binding.textView.background == null) {
//                holder.binding.textView.setBackgroundColor(Color.rgb(Random.nextInt(255), Random.nextInt(255), Random.nextInt(255)))
            }
        }

    }

    private class Holder(val binding: FragmentDanmuItemBinding) : RecyclerView.ViewHolder(binding.root)
}

class CircleDrawable(mBitmap: Bitmap) : Drawable() {
    private val mPaint: Paint = Paint()

    private var mWidth: Int = 0

    init {
        val shader = BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        mPaint.isAntiAlias = true
        mPaint.shader = shader
        mWidth = min(mBitmap.width, mBitmap.height)
    }

    override fun draw(canvas: Canvas) {
        canvas.drawCircle(mWidth / 2f, mWidth / 2f, mWidth / 2f, mPaint)
    }

    override fun setAlpha(alpha: Int) {
        mPaint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mPaint.colorFilter = colorFilter
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }

    override fun getIntrinsicWidth(): Int = mWidth

    override fun getIntrinsicHeight(): Int = mWidth
}
